#include "LobbyScene.h"
#include "curl/curl.h"
#include "NetworkManager.h"
#include "JsonObject.h"
#include "PlayScene.h"
#include "GameManager.h"
#include "RoundSummaryView.h"

USING_NS_CC;

CCScene* LobbyScene::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    LobbyScene *layer = LobbyScene::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool LobbyScene::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }

	CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    CCSprite* pBG = CCSprite::create("game_bg.png");
	pBG->setAnchorPoint(CCPoint(0.5f, 0.5f));
	pBG->setPosition(CCPoint(winSize.width / 2, winSize.height / 2));
	this->addChild(pBG);

	CCMenu* pMenu = CCMenu::create();
	this->addChild(pMenu);

	mNewGameButton = CCMenuItemImage::create("new_game_cell_active.png", "new_game_cell_inactive.png", this, menu_selector(LobbyScene::newGameCallback));
	mNewGameButton->setScale(0.5f);
	mNewGameButton->setPosition(CCPoint(0.0f, 100.0f));

	pMenu->addChild(mNewGameButton);

	CCLabelTTF* pNewLabel = CCLabelTTF::create("New Game", "Arial", 40.0f);
	pNewLabel->setPosition(CCPoint(150.0f, 30.0f));
	mNewGameButton->addChild(pNewLabel, 0, 0);

    return true;
}

void LobbyScene::newGameCallback(CCObject* pSender)
{	
	int unsigned nameId = rand();
	char szName[6] = {"\0"};
	sprintf(szName, "%d", nameId);

	std::map<std::string, std::string> paraList;
	paraList.insert(std::make_pair("your_name", szName));

	NetworkManager::getInstance()->call(
		"request_newgame"
		,this
		,callfuncO_selector(LobbyScene::onCreateGameSucceed)
		,callfuncO_selector(LobbyScene::onCreateGameFailed)
		,&paraList
		);
}

void LobbyScene::handleMessage(CCObject* pSender)
{
	JsonObject* pJson = dynamic_cast<JsonObject*>(pSender);
	CCAssert( pJson ,"invalid type!");
	if( pJson )
	{
		Json::Value& jsonval = pJson->getJsonVal();

		int msgType = jsonval["msgType"].asInt();		

		mNewGameButton->setEnabled(false);
		CCLabelTTF* pLabel = (CCLabelTTF*)(mNewGameButton->getChildByTag(0));

		GameManager& gameManager = GameManager::getInstance();

		if(jsonval.has("msgContent"))
		{
			Json::Value& jsonvalContent = jsonval["msgContent"];
			if(jsonvalContent.has("yourCookie"))
			{
				gameManager.mPlayerCookie = jsonvalContent["yourCookie"].asString();
			}
		}

		if(msgType == Msg::GameRequestCreated)
		{
			schedule(schedule_selector(LobbyScene::updateRequest), 5.0f);

			pLabel->setString("waiting...");				
		}
		else if(msgType == Msg::GameUpdate)
		{
			assert(jsonval.has("msgContent"));
			Json::Value& jsonvalContent = jsonval["msgContent"];
			
			gameManager.mCurrentGameCookie = jsonvalContent["gameCookie"].asString();
			gameManager.create(gameManager.mCurrentGameCookie);

			// switch to roundSummaryView Scene
			CCScene* pScene = new CCScene();
			pScene->autorelease();

			RoundSummaryView* roundSummaryScene = RoundSummaryView::create();

			pScene->addChild(roundSummaryScene);

			CCTransitionFade * transitionScene = CCTransitionFade::create( 1.0f, pScene, ccWHITE );
			CCDirector::sharedDirector()->replaceScene(transitionScene);
		}
	}
}

void LobbyScene::onCreateGameSucceed(CCObject* pSender)
{
	handleMessage(pSender);
}

void LobbyScene::onCreateGameFailed(CCObject* pSender)
{
	assert(0);
}

void LobbyScene::onResumeCallback(CCObject* pSender)
{
	std::map<std::string, std::string> paraList;
	paraList.insert(std::make_pair("yourCookie", "Au9QDF"));

	NetworkManager::getInstance()->call(
		"UpdateRequest",
		this,
		callfuncO_selector(LobbyScene::onUpdateRequestSucceed),
		callfuncO_selector(LobbyScene::onUpdateRequestFailed),
		&paraList
		);
}

void LobbyScene::updateRequest(float deltaTime)
{
	unschedule(schedule_selector(LobbyScene::updateRequest));

	std::map<std::string, std::string> paraList;
	paraList.insert(std::make_pair("yourCookie", GameManager::getInstance().mPlayerCookie));

	NetworkManager::getInstance()->call(
		"UpdateRequest"
		,this
		,callfuncO_selector(LobbyScene::onUpdateRequestSucceed)
		,callfuncO_selector(LobbyScene::onUpdateRequestFailed)
		,&paraList
		);
}

void LobbyScene::onUpdateRequestSucceed(CCObject* pSender)
{
	handleMessage(pSender);	
}

void LobbyScene::onUpdateRequestFailed(CCObject* pSender)
{
	assert(0);
}

